package com.example.demo2.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.demo2.common.Result;
import com.example.demo2.entity.Customer;
import com.example.demo2.entity.DTO.CustomerDTO;
import com.example.demo2.entity.DTO.FilesDTO;
import com.example.demo2.entity.Files;
import com.example.demo2.service.FilesService;
import org.apache.tomcat.util.MultiThrowable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("files")
public class FilesController {
    // 在application.yml动态获取上传图片的存储路径
    @Value("${files.upload.path}")
    private String basePath;
    //上传
    @Resource
    public FilesService fileService;
    @PostMapping("/uploadFile")
    public Result upload(MultipartFile multipartFile) throws IOException {
        String url = fileService.upload(basePath,multipartFile);
        return Result.success(url);
    }
    //下载
    @GetMapping("/{fileUUID}")
    public void download(@PathVariable String fileUUID, HttpServletResponse response) throws IOException{
        if (StringUtils.isNotEmpty(fileUUID)){
            fileService.download(basePath,fileUUID,response);
        }
    }
    // 删除
    @PostMapping("/delete")
    public Result deleteCustomer(@RequestBody Customer customer) {
        return Result.success(fileService.removeById(customer.getId()));
    }
    //批量删除
    @PostMapping("/batch/delete")
    public Result batchDeleteCustomer(@RequestBody List<Integer> ids) {
        return Result.success(fileService.removeBatchByIds(ids));
    }
    //分页查询
    @PostMapping("/findPage")
    public Result findPage(@RequestBody FilesDTO filesDTO) {
        return Result.success(fileService.findPageOfFiles(filesDTO));
    }


//    @Value("${files.upload.path}")
//    private String fileUploadPath;
//    @Autowired
//    private FilesService filesService;


//    //    上传
//    @PostMapping("/upload")
//    public Result upload(@RequestParam MultipartFile files) throws IOException {
//        String originalFilename = files.getOriginalFilename();
//        String type = FileUtil.extName(originalFilename);
//        long size = files.getSize() / 1024;//取kb
//        //先存磁盘
//        File uploadFile = new File(fileUploadPath);
//        System.out.println("fileUploadPath" + fileUploadPath);
//        if (!uploadFile.exists()) {
//            uploadFile.mkdirs();
//        }
////        定义一个文件唯一的标识uuid
//        String uuid = IdUtil.fastSimpleUUID();
//        String fileUUID = uuid + StrUtil.DOT + type;
//        File realFile = new File(fileUploadPath + fileUUID);
//
//        files.transferTo(realFile);
//        String url = "http://127.0.0.1:9090/files/" + fileUUID;
//        //再存到数据建库
//        Files dbFile = new Files();
//        dbFile.setName(originalFilename);
//        dbFile.setType(type);
//        dbFile.setSize(size);
//        dbFile.setUrl(url);
//        filesService.save(dbFile);
//        return Result.success(url);
//    }
//
//    @GetMapping("/{fileUUID}")
//    public void downloadFile(@PathVariable String fileUUID, HttpServletResponse response) throws IOException {
//        //读出流格式
//        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileUUID, "UTF-8"));
//        response.setContentType("application/octet-stream");
//        // 根据文件的唯一标识获取文件
//        File uploadFile = new File(fileUploadPath + fileUUID);
//        ServletOutputStream os = response.getOutputStream();
//        os.write(FileUtil.readBytes(uploadFile));
//        os.flush();
//        os.close();
//
//    }

}
